
**************************************************************************************************************************************
*****                                                                                                                            *****
***** Does personal experience lead to better investment outcomes? Evidence from EDGAR search activities in Las Vegas            *****
*****                                                                                                                            *****              
*****                                                        AUG 2023                                                            *****
*****                                                                                                                            *****
*****                                                                                                                            *****
**************************************************************************************************************************************

***********************
****    CONTENTS   ****
***********************
*1) Import Dataset
*2) Construct Figure Data
*3) Construct Summary Statistics - Tables 1 & 2
*4) Construct Fama Macbeth Regression Results - Tables A2 & A3

/*************************************************************/
/* Section 1: Import Datasets*/
/*************************************************************/
/*Designation Library Name File Path*/
libname vegas "ENTER FILE PATH LOCATION OF DATASETS";

data vegas_data; set vegas.vegas_data_fm; run;
data ipaccess; set vegas.ipaccess_fm; run;

/*************************************************************/
/* Section 2: Figure Data*/
/*************************************************************/

/******Figure 2 Dataset*****/
/******Access By Month******/

data month_summary; set vegas_data; month=month(date); run;
Proc sql; 
	create table monthsummary1 as
	select month, sum(vegas_access) as access_number, sum(vegas_ind) as access_months
	from month_summary
	group by month order by month;
quit;
data Figure2; set Monthsummary1; run; 
proc sql; drop table month_summary, Monthsummary1; quit;
proc print data=Figure2; run;


/******Figure 1 & 3 Dataset*****/
/******Access by Hour and Hotel*/

/*Access by Casino*/
Proc sql;
	create table Figure1 as
	select namebusinessentity, count(*) as access
	from ipaccess
	group by namebusinessentity
	order by namebusinessentity;
quit;

Proc sql; 
	create table Figure3 as 
	select vhour, count(*) as access 
	from ipaccess 
	group by vhour 
	order by vhour; 
quit;

proc print data=Figure1; run;
proc print data=Figure3; run;

/******Figure 4 Dataset*****/
/******Univariate Table*****/
Proc sql; create table vegas_only as select distinct permno, gvkey, mtime, vegas_access, vegas_firm 
	from vegas_data where vegas_ind=1 order by permno, gvkey, mtime; quit; 

/*193,272 Total Access Events / 153,919 Total Access Events of Vegas Related Firms (79.64% of Total Access Events)*/
Proc sql; create table sum1 as select sum(vegas_access) as overall from vegas_only; quit;
Proc sql; create table sum2 as select sum(vegas_access) as vegasrelated from vegas_only where vegas_firm=1; quit;

Proc sql; create table universe as select distinct permno, gvkey, mtime, totalcikaccess, totalaccess, vegas_firm 
	from vegas_data order by permno, gvkey, mtime; quit; 

/*7,515,745,277 Total Access Events / 378,533,234 Total Access Events of Vegas Related Firms (5.04% of Total Access Events)*/
Proc sql; create table sum1 as select sum(totalcikaccess) as overall from universe; quit;
Proc sql; create table sum2 as select sum(totalcikaccess) as overall from universe where vegas_firm=1; quit;

Proc sql; drop table sum1, sum2, universe, vegas_only; quit;

/*************************************************************/
/* Section 3: Tables 1 & 2 Summary Statistics */
/*************************************************************/

/*Table 1*/
/*Access By Industry*/
Proc sql;
	create table industrysummary1 as
	select ffi48, sum(vegas_access) as vegas
	from vegas_data
	group by ffi48
	order by ffi48;
quit;
Proc sql;
	create table industrysummary2 as
	select ffi48, sum(vegas_ind) as access_months
	from (select distinct ffi48, mtime, vegas_ind from vegas_data)
	group by ffi48
	order by ffi48;
quit;
Proc sql; 
	create table industry as 
	select a.*, b.*
	from industrysummary1 a left outer join industrysummary2 b
		on a.ffi48=b.ffi48
	order by ffi48;
quit;
proc sql; drop table industrysummary1, industrysummary2; quit;

Proc sql; create table industrysummary1 as select distinct ffi48, permno, gvkey from vegas_data order by ffi48; quit;

/*Note: 6,230 is the total number of access observation in industrysummary1 dataset*/
Proc sql; create table industrysummary1 as select ffi48, count(*)/6230 as sample_pct from industrysummary1 group by ffi48 order by ffi48; quit;
Proc sql; create table industry as select a.*, b.* from industry a left outer join industrysummary1 b on a.ffi48=b.ffi48 order by ffi48; quit;
proc sql; drop table industrysummary1; quit;

Proc sql; create table industrysummary1 as select distinct ffi48, permno, gvkey, vegas_ind from vegas_data
	order by ffi48, permno, gvkey, vegas_ind; quit;
data industrysummary1; set industrysummary1; by ffi48 permno gvkey; if last.gvkey; run;
Proc sql; create table industrysummary1 as select ffi48, count(*) as firms, sum(vegas_ind) as access from industrysummary1 group by ffi48 order by ffi48; quit;
data industrysummary1; set industrysummary1; pct_by_industry = access/firms; run;
Proc sql; create table industry as select a.*, b.pct_by_industry from industry a left outer join industrysummary1 b on a.ffi48=b.ffi48 order by ffi48; quit;
proc sql; drop table industrysummary1; quit;

Proc sql; create table industrysummary1 as select distinct ffi48, permno, gvkey, date, vegas_ind from vegas_data
	order by ffi48, permno, gvkey, vegas_ind; quit;
Proc sql; create table industrysummary1 as select ffi48, permno, gvkey, count(*) as obs, sum(vegas_ind) as access 
	from industrysummary1 group by ffi48, permno, gvkey order by ffi48, permno, gvkey; quit;
data industrysummary1; set industrysummary1; pct_month_access = access/obs; run;
Proc sql; create table industrysummary1 as select ffi48, mean(pct_month_access) as pct_month_access
	from industrysummary1 group by ffi48 order by ffi48; quit;
Proc sql; create table industry as select a.*, b.pct_month_access from industry a left outer join industrysummary1 b on a.ffi48=b.ffi48 order by ffi48; quit;
proc sql; drop table industrysummary1; quit;

data Table1; set industry; run;
proc sql; drop table industry; quit;
proc print data=Table1; run;

/*Table 2*/
Proc means data=vegas_data n mean std p25 median p75; 
	var vegas_access vegas_ind vegas_firm f6 allfilings gs earn ra lmcap lmb roa_w lev_w ret12_w std12_w io_w lag_idiovol_w ret1 ret3 ret6 ret121 bhar1m bhar3m bhar6m bhar12m;
quit;

/*************************************************************/
/*Section 4: Fama MacBeth Regressions - Table A2 & A3*/
/*************************************************************/
data fmdata; set vegas_data; run;
%macro ind();
	%Do i=1 %to 48;
		data fmdata; set fmdata; if ffi48=&i then _&i=1; else _&i=0; run;
	%end;
%mend;
%ind;

%macro fm(DV=, LAGS=, FMVARS=, fmcount=, outputname=);

		%let lines=%eval(&fmcount*2+3);

		data &outputname; do index = 1 to &lines; output; end; run;
		%do i=1 %to &fmcount;
			%let j=%eval(&i*2);
			data &outputname; set &outputname; 
				length factor $15.; format factor $char15.;
				if index in (%eval(&j-1),&j) then factor="%scan(&FMVARS.,&i.)";
			run;
		%end;
		data &outputname; set &outputname; if index in (%eval(&lines-2),%eval(&lines-1)) then Factor="Intercept"; run;
		data &outputname; set &outputname; if index in (&lines) then Factor="RSQ"; run;
		
		%do k=1 %to 4;
			proc sort data=fmdata; by mtime; run;
			ods output FitStatistics=Fit;
			Proc reg data=fmdata outest=fm_est; by mtime; 
				model %scan(&DV.,&k.) = &FMVARS _1-_48; 
			run;
			ods listing close; run; quit;
			Proc sql; create table fit as select &lines as index, Label2 as variable, mean(nValue2) as var1 from fit where Label2="R-Square" group by label2; quit;
			
			%do i=1 %to &fmcount;
				%let j=%eval(&i*2);
				proc model data=fm_est; instruments / intonly; %scan(&FMVARS.,&i.)=a0; fit %scan(&FMVARS.,&i.) / gmm kernel=(bart,%scan(&LAGS.,&k.),0) vardef=n;
				ods output ParameterEstimates=tstats; ods listing close; run; quit;
				data tstats; set tstats; Variable="%scan(&FMVARS.,&i.)"; index=%eval(&j-1); run;
				proc append data=tstats base=fm force; run;
			%end;

			proc model data=fm_est; instruments / intonly; intercept=a0; fit intercept / gmm kernel=(bart,%scan(&LAGS.,&k.),0) vardef=n;
			ods output ParameterEstimates=tstats; ods listing close; run; quit;
			data tstats; set tstats; Variable="intercept"; index=%eval(&lines-2); run;
			proc append data=tstats base=fm force; run;

			data fm; set fm;
				length ar1_txt $15.; format ar1_txt $char15.;
				length mean1_txt $15.; format mean1_txt $char15.;
				ar1_txt=put(tValue,12.2);
				if probt<.01 then mean1_txt=catx("",put(estimate,12.3),'***');
				else if probt<.05 then mean1_txt=catx("", put(estimate,12.3),'**');
			 	else if probt<.1  then mean1_txt=catx("", put(estimate,12.3),'*');
			    else mean1_txt=catx("", put(estimate,12.3), '   ');
			drop estimate Parameter EstType DF StdErr probt tvalue; 
			run;

			data fm2; set fm; keep Variable index ar1_txt; rename ar1_txt = var; index=index+1; run;
			data fm1; set fm; keep Variable index mean1_txt; rename mean1_txt = var; run;
			data fm3; set fm1 fm2; run;
			proc sort data=fm3; by index; run;
			data fit; set fit; var=put(var1,12.3); drop var1; run;
			data fm3; set fm3 fit; run;
			Proc sql; create table &outputname as select a.*, b.var as %scan(&DV.,&k.) from &outputname a left outer join fm3 b on a.index=b.index order by index; quit;
			proc sql; drop table fm, fm_est, tstats, fm1, fm2, fm3, fit; quit;
		%end;
%mend;

/*Macro Variable Definitions*/
/*
DV = List of Dependent Variables
LAGS = Number of lags (to be used with each respective DV) to use for Newey-West t-statistics
FMVARS = List of independent variables
FMCOUNT = Number of independent variables 
outputname = Name assigned to output dataset
*/

%fm(DV=ret1 ret3 ret6 ret121, LAGS=3 3 6 12, FMVARS=vegas_ind log_f6 gs earn ra lmcap lmb roa_w lev_w ret12_w std12_w lag_idiovol_w io_w, fmcount=13, outputname=TableA2);
%fm(DV=ret1 ret3 ret6 ret121, LAGS=3 3 6 12, FMVARS=vegas_ind vegas_firm vegas_firm_interaction log_f6 gs earn ra lmcap lmb roa_w lev_w ret12_w std12_w lag_idiovol_w io_w, fmcount=15, outputname=TableA3);

Proc print data=TableA2; run;
Proc print data=TableA3; run;

proc sql; drop table fmdata; quit;

